package com.health.mapp.impl;

import java.awt.image.BufferedImage;
import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.health.db.model.PatientShortInfo;
import com.health.db.service.user.IPatientShortInfoService;
import com.health.db.service.user.IUserInfoService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryUserQrCodeReq;
import com.health.mapp.bean.QueryUserQrCodeResp;
import com.health.mapp.service.ImgUpload;
import com.health.mapp.utils.PicMain;
import com.health.mapp.utils.QRCodeUtil;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/***
 * 查询qrcode
 * @author YC
 *
 */
public class QueryUserQrCodeImpl  implements ActionInterface{
	
	
	@Autowired
	private IPatientShortInfoService patientShortInfoService;
	
	private final String DIR ="qrcode_share/";
	  
	private final String DIR2 ="barcode_share/";
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryUserQrCodeReq req = (QueryUserQrCodeReq) reqBean;
		QueryUserQrCodeResp _resp = new QueryUserQrCodeResp();
		
		String str = req.getName()+"_"+req.getSex() +"_"+req.getBirthday()+"_"+req.getPname()+"_"+req.getPhone()+"_"+req.getAddress();
		
		PicMain tt = new PicMain();  
		
        String path = this.getClass().getClassLoader().getResource("").getPath()+"resources/backgroud.png";
        
        String uploaddesc =  this.getClass().getClassLoader().getResource("").getPath()+"upload/";
        
        File dir = new File(uploaddesc);
        if (!dir.exists()) {
            dir.mkdir();
        }
        //selectMedicalCardNoKey
        String medicalCardNo = patientShortInfoService.selectMedicalCardNoKey();
        
        //塞入患者资料
        PatientShortInfo  patientShortInfo = new PatientShortInfo();
        patientShortInfo.setBirthdayStr(req.getBirthday());
        patientShortInfo.setName(req.getName());
        patientShortInfo.setParentName(req.getPname());
        patientShortInfo.setPhone(req.getPhone());
        patientShortInfo.setHomeAddress(req.getAddress());
        patientShortInfo.setSex(req.getSex());
        patientShortInfo.setMedicalCardNo(medicalCardNo);
        
        _resp.setMedicalCardNo(medicalCardNo);
        _resp.setName(req.getName());
        
        long timeradom= System.currentTimeMillis();
        
        String uploadfile = uploaddesc+"/"+timeradom+".jpg";
        
        String uploadBarFile =  uploaddesc+"/"+timeradom+".png";
        
        //二维码
        BufferedImage d = QRCodeUtil.createImage(str, "", true,path); 
        //条形码
        QRCodeUtil.createBarCodeImg(medicalCardNo, uploadBarFile);
        
        tt.writeImageLocal(uploadfile, d  );  
        
        //文件上传
        File file = new File(uploadfile);
        ImgUpload.uploadByOssShare(file, DIR, timeradom+".jpg");
        file.delete();
        
        File file2 = new File(uploadBarFile);
        ImgUpload.uploadByOssShare(file2, DIR2, timeradom+".png");
        file2.delete();
        
        patientShortInfo.setQrCodeUrl(DIR+timeradom+".jpg");
        patientShortInfo.setQrCodeFullUrl("http://liqiao.shouerhealth.cn/qrcode_share/"+timeradom+".jpg");
        patientShortInfo.setBarCodeUrl(DIR2+timeradom+".png");
        patientShortInfo.setBarCodeFullUrl("http://liqiao.shouerhealth.cn/barcode_share/"+timeradom+".png");
        
        
        
         patientShortInfoService.insert(patientShortInfo,req.getUserId());
        
		_resp.setStr(str);
		_resp.setQrCodeUrl(DIR+timeradom+".jpg");
		_resp.setQrCodeFullUrl("http://liqiao.shouerhealth.cn/qrcode_share/"+timeradom+".jpg");
		
		_resp.setBarCodeUrl(DIR2+timeradom+".png");
		_resp.setBarCodeFullUrl("http://liqiao.shouerhealth.cn/barcode_share/"+timeradom+".png");
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
		
		return _resp;
	}

	public IPatientShortInfoService getPatientShortInfoService() {
		return patientShortInfoService;
	}

	public void setPatientShortInfoService(
			IPatientShortInfoService patientShortInfoService) {
		this.patientShortInfoService = patientShortInfoService;
	}

	
	
}
